package com.example.hm_dtfkz.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.dq.ad.AdManager;
import com.dq.ad.UMManager;
import com.example.hm_dtfkz.R;
import com.example.hm_dtfkz.config.LoginConfig;
import com.example.hm_dtfkz.databinding.DialogAnswerCorrectBinding;
import com.example.hm_dtfkz.util.ActivityClickUtil;
import com.example.hm_dtfkz.util.AnimUtil;
import com.example.hm_dtfkz.util.DisplayUtil;
import com.example.hm_dtfkz.util.GuideComponent;

/**
 * FileName: DialogAnswerCorrect
 * Author: admin
 * Date: 2021/11/8 9:39
 * Description:
 */
public class DialogAnswerCorrect extends Dialog implements View.OnClickListener {

    private DialogAnswerCorrectBinding correctBinding;
    private final Activity activity;
    private OnClickLister onClickLister;
    private double money = 0;

    public DialogAnswerCorrect(@NonNull Activity a) {
        super(a, R.style.dialog_loading);
        this.activity = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        correctBinding = DialogAnswerCorrectBinding.inflate(getLayoutInflater());
        setContentView(correctBinding.getRoot());
        getWindow().getDecorView().setPadding(0, 0, 0, 0);

        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = DisplayUtil.getScreenWidth(activity);
        lp.height = DisplayUtil.getScreenHeight(activity);
        getWindow().setAttributes(lp);

        setCancelable(false);
        setCanceledOnTouchOutside(false);
        initView();
        initData();
        initLister();
    }

    private void initView() {

//        correctBinding.tvReceive.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        AnimUtil.receiveAnimM(activity, correctBinding.imgReceiveX3);
        AnimUtil.rotateAnim(correctBinding.imgCenterBg);

        if (LoginConfig.getGuideIndex() == 1) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    showGuideView();
                }
            }, 100);
        }
        else {
            AdManager.getInstance().showNativeAD(activity, correctBinding.layoutBanner, DisplayUtil.getScreenWidthDp(activity) - 48, 0, AdManager.NATIVE_AD_TYPE_UP_IMAGE, false, new AdManager.OnNativeVideoADListener() {
                @Override
                public void onSuccess() {
                    UMManager.onEvent(UMManager.Event.SF_NA_success);
                    correctBinding.layoutBanner.setBackgroundColor(activity.getResources().getColor(R.color.Color_FFFFFF));
                }

                @Override
                public void onClosed() {
                    correctBinding.layoutBanner.setBackgroundColor(activity.getResources().getColor(android.R.color.transparent));
                }

                @Override
                public void onError() {
                    UMManager.onEvent(UMManager.Event.SF_NA_fail);
                }

                @Override
                public void onClick() {
                    UMManager.onEvent(UMManager.Event.SF_NA_click);
                }

                @Override
                public void onShow(View view) {

                }
            });
        }
    }

    public void showGuideView() {
        correctBinding.textDesc.setVisibility(View.GONE);
        correctBinding.layoutAd.setVisibility(View.GONE);

        GuideComponent comp = new GuideComponent();
        comp.guideIndex = 1;
        View compView = comp.getView(getLayoutInflater());

        ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.leftToLeft = R.id.imgReceive_x3;
        params.topToTop = R.id.imgReceive_x3;
        compView.setLayoutParams(params);
        correctBinding.layoutBg.addView(compView);
    }

    @SuppressLint("SetTextI18n")
    private void initData() {
        correctBinding.tvHbq.setText("红包券 x " + (int) this.money);
//        correctBinding.tvHbqx3.setText(" x " + (int) (this.money * 3));

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                correctBinding.imgClose.setVisibility(View.VISIBLE);
            }
        }, 3000);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                correctBinding.tvGiveup.setVisibility(View.VISIBLE);
            }
        }, 500);

    }

    private void initLister() {
        correctBinding.imgReceiveX3.setOnClickListener(this);
        correctBinding.tvGiveup.setOnClickListener(this);
        correctBinding.imgClose.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (ActivityClickUtil.getInstance().isFastClick()) {
            //防止用户快速点击
            return;
        }
        dismiss();
        if (v.getId() == R.id.imgReceive_x3) {
            UMManager.onEvent(UMManager.Event.SF_open);
            if (this.onClickLister != null) {
                if (LoginConfig.getGuideIndex() == 1) {
                    LoginConfig.setGuideIndex(2);
                }
                this.onClickLister.onDoubleReceive();
            }
        } else if (v.getId() == R.id.tvGiveup) {
            UMManager.onEvent(UMManager.Event.SF_click_giveup);
            if (this.onClickLister != null) {
                this.onClickLister.onClose();
            }
        } else if (v.getId() == R.id.imgClose) {
            UMManager.onEvent(UMManager.Event.SF_click_close);
            if (this.onClickLister != null) {
                this.onClickLister.onClose();
            }
        }
    }

    public DialogAnswerCorrect setMoney(double m) {
        this.money = m;
        return this;
    }

    public DialogAnswerCorrect setOnClickLister(OnClickLister clickLister) {
        this.onClickLister = clickLister;
        return this;
    }

    public interface OnClickLister {
        void onDoubleReceive();

        void onClose();
    }
}
